package guestbook;

import java.util.*;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.repackaged.com.google.common.collect.Lists;

public class PutHelper
{
	public Context context;
	public List<Entity> entities = Lists.newArrayList();

	public PutHelper(Context context)
	{
		this.context = context;
	}

	public void put(java.lang.Iterable<Entity> entities)
	{
		while (entities.iterator().hasNext())
		{
			Entity entity = entities.iterator().next();
			setGlobalPropertiesOnEntity(entity);
			this.entities.add(entity);
		}
	}

	public void put(Collection<Entity> entities)
	{
		for(Entity entity : entities)
			setGlobalPropertiesOnEntity(entity);
		
		this.entities.addAll(entities);
	}

	public void put(Entity entity)
	{
		setGlobalPropertiesOnEntity(entity);
		this.entities.add(entity);
	}
	
	public void setGlobalPropertiesOnEntity(Entity entity)
	{
		if(!entity.getKey().isComplete())
			entity.setProperty("DateCreated", System.currentTimeMillis());
	}
}